package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"time"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:9999")
	if err != nil {
		panic(err)
	}

	//scanner := os.Stdin
	f, err := os.Open("D:\\htdocs\\go\\src\\go-zinx\\demo\\v0.2\\data.dat")
	if err != nil {
		panic(err)
	}

	scanner := bufio.NewReader(f)

	go func() {
		for {
			str, err := scanner.ReadString('\n')
			if err != nil {
				return
			}
			log.Printf("writer %s len %d", str, len(str))
			// 写出的时候,  需要指定写出长度
			_, err = conn.Write([]byte(str))
			if err != nil {
				return
			}
			time.Sleep(1 * time.Second)
		}

	}()

	for {
		// todo: 服务端多次write时, 只读到了一次的数据
		rBuf := make([]byte, 512)
		rcnt, err := conn.Read(rBuf)
		if err != nil {
			break
		}
		log.Printf("read  %s len %d\n", rBuf, rcnt)
	}

}
